package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.templates.OssTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.domain.vo.VideoVo;
import com.tanhua.domain.vo.VoiceVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.VoiceApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Alfred
 * @Date: 2021/04/20/9:39
 * @Description:
 */
@Service
public class VoiceService {

    @Autowired
    private OssTemplate ossTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private VoiceApi voiceApi;

    /**
    * @Description: 桃花传音-保存语音
    * @Param: [soundFile]
    * @return: void
    * @Author: Alfred
    * @Date: 2021/4/20
    */
    public void sendVoice(MultipartFile soundFile) {
        try {
            //获取登录用户的id
            Long userLoginId = UserHolder.getUserId();

            //获取文件后缀
            String originalFilename = soundFile.getOriginalFilename();//获取原始文件名
            //a.将视频封面上传阿里云，得到图片url地址
            String soundUrl = ossTemplate.upload(originalFilename, soundFile.getInputStream());

            //获取性别
            UserInfo userInfo = userInfoApi.findByUserId(userLoginId);
            String gender = userInfo.getGender();


            Voice voice = new Voice();
            voice.setSoundUrl(soundUrl);
            voice.setGender(gender);
            voice.setUserId(userLoginId);

            voiceApi.save(voice);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Description: 接受语音
    * @Param: []
    * @return: com.tanhua.domain.vo.VoiceVo
    * @Author: Alfred
    * @Date: 2021/4/20
    */
    public VoiceVo getVoice() {
        //获取登录用户id
        Long userLoginId = UserHolder.getUserId();

        Voice voice = voiceApi.getVoice();
        UserInfo userInfo = userInfoApi.findByUserId(voice.getUserId());

        VoiceVo voiceVo = new VoiceVo();
        BeanUtils.copyProperties(userInfo,voiceVo);

        voiceVo.setVocieUrl(voice.getSoundUrl());


        return null;
    }
}
